<?php
class PNRecommendItemArray extends PNObjectArray {
  function PNRecommendItemArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'ishopstore_item';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    $this->_init($init, $where);
  }

  function genSort(){
    $sort  = FormUtil::getPassedValue ('order', false);
    $result = false;
    if ($result){
      return $result;
    }else{
      //default order

      return 'ORDER BY itm_cr_date DESC';
    }
  
  }

  function genFilter(){
    $filter  = FormUtil::getPassedValue ('filter', false);
    $keyword = DataUtil::formatForStore($filter['keyword']);
    $type = DataUtil::formatForStore($filter['type']);

    $category_id = DataUtil::formatForStore($filter['category_id']);
    $type_id = DataUtil::formatForStore($filter['type_id']);

    $wheres = array();
    //filter by user_id
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    if ($user_id) $wheres[] = "itm_cr_uid = '$user_id'";
    //filter by keyword
    if ($keyword && $type){
      switch($type){
        case 'name': $wheres[] = "itm_name LIKE '%". $keyword ."%'"; break;
        case 'title': $wheres[] = "itm_title LIKE '%". $keyword ."%'"; break;
        case 'category_name': $wheres[] = "cty_name LIKE '%". $keyword ."%'"; break;
      }
    }
    //filter by selector
    if ($category_id && intval($category_id)) $wheres[] = "itm_category_id ='$category_id'";
    if ($type_id && intval($type_id)) $wheres[] = "itm_type_id ='$type_id'";
    //$wheres[] = "itm_cr_uid = '2' ";
    // filter for Recomend item
    $wheres[] = "itm_recommend = '9'";
    return implode(' AND ', $wheres) ;
  }

  function selectExtendResult(){
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    $result = array();
    if ($user_id){
      $result['user'] = InnoUtil::getUserInfo($user_id);
    }

      $result['firstimages'] = DBUtil::selectObject('ishopstore_item' , "WHERE itm_id = '$id'" ,array('id','firstimage','firsthumbimage'));

    return $result;
  }
}